package com.tjsinfo.tjsoa.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.system.vo.TSystemRolesGroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUserRoleGroup;
import com.tjsinfo.tjsoa.system.vo.TjsRoles;

public class RoleGroupManageAction extends DispatchAction {

	private IQbcDao qbcDao;

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	//权限组列表
	public ActionForward roleGroupList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {

			DetachedCriteria criteria = DetachedCriteria.forClass(TjsRoles.class);
			// 当前项目所有的menu
			List<TjsRoles> rolesList = qbcDao.getCriteria(criteria);
			// 所有的父栏目,并从集合中清楚父栏目
			Map<Integer, String> parentMenu = new HashMap<Integer, String>();//父栏目
			for (Iterator iterator = rolesList.iterator(); iterator.hasNext();) {
				TjsRoles tjsRoles = (TjsRoles) iterator.next();
				if (tjsRoles.getParentId() == null|| tjsRoles.getParentId().equals("")){
					parentMenu.put(tjsRoles.getId(), tjsRoles.getName());
					iterator.remove();
				}
			}
			// 匹配父栏目与子栏目
			Map<String, List<TjsRoles>> parentMenuMap = new HashMap<String, List<TjsRoles>>();//栏目map
			for (TjsRoles tjsRoles : rolesList) {
					String parentName=parentMenu.get(tjsRoles.getParentId());
					if(!parentMenuMap.containsKey(parentName)){
						List<TjsRoles> list=new ArrayList<TjsRoles>();
						list.add(tjsRoles);
						parentMenuMap.put(parentName,list);
					}else{
						List<TjsRoles> list=parentMenuMap.get(parentName);
						list.add(tjsRoles);
						parentMenuMap.put(parentName,list);
					}
			}
			request.setAttribute("parentMenuMap", parentMenuMap);
			
			DetachedCriteria roleCriteria = DetachedCriteria.forClass(TSystemRolesGroup.class);
			roleCriteria.addOrder(Order.asc("ordernumber"));
			List<TjsRoles> rolesGroupList = qbcDao.getCriteria(roleCriteria);
			request.setAttribute("rolesGroupList", rolesGroupList);
			return mapping.findForward("roleGroupJspList");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
	//添加权限组
	public ActionForward roleGroupSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {

			String groupName=request.getParameter("groupName").trim().toString();
			String groupNo=request.getParameter("groupNo").trim().toString();

			DetachedCriteria criteriaAll = DetachedCriteria.forClass(TSystemRolesGroup.class);
			SimpleExpression a = Restrictions.eq("groupname", groupName);
			criteriaAll.add(a);
			List<TSystemRolesGroup> roleList=qbcDao.getCriteria(criteriaAll);
			if(roleList.size()==0){
			
			int no=0;
			try {
				no=Integer.valueOf(groupNo);
			} catch (Exception e) {
			}
			
			TSystemRolesGroup trg=new TSystemRolesGroup();
			trg.setGroupname(groupName);
			trg.setOrdernumber(no);
			trg.setPermission(",");
			
			qbcDao.save(trg);
			}else{
				request.setAttribute("alert", "当前权限组名称已经存在!");
			}
			return mapping.findForward("roleGroupList");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
	//更新权限组
	public ActionForward roleGroupUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String id=request.getParameter("nowSelectGroupId");
			
			SimpleExpression a = Restrictions.eq("id", Integer.valueOf(id));
			DetachedCriteria criteria = DetachedCriteria.forClass(TSystemRolesGroup.class);
			criteria.add(a);
			List<TSystemRolesGroup> groupList = qbcDao.getCriteria(criteria);
			groupList.get(0).setPermission(request.getParameter("selectGroupId"));
			TSystemRolesGroup trg=groupList.get(0);
			
			DetachedCriteria criteriaAll = DetachedCriteria.forClass(TSystemRolesGroup.class);
			SimpleExpression as = Restrictions.eq("id", trg.getId());
			criteriaAll.add(as);
			List<TSystemRolesGroup> roleList=qbcDao.getCriteria(criteriaAll);
			if(roleList.size()!=0){
				String oldRole=roleList.get(0).getPermission();
				String newRole=trg.getPermission();
				String addRole=DealString.addRoleString(oldRole, newRole);//新增权限
				String removeRole=DealString.removeRoleString(oldRole, newRole);//移除权限
				String[] addRoleArray=addRole.split(",");
				String[] removeRoleArray=removeRole.split(",");
			}
			qbcDao.update(trg);
			return mapping.findForward("roleGroupList");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
	//
	public ActionForward roleGroupPermisssionUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			
			return mapping.findForward("roleGroupList");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
	//删除权限组,删除t_system_userRoleGroup表中对应的权限组
	public ActionForward roleGroupDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Integer id=Integer.parseInt(request.getParameter("id"));
			SimpleExpression a = Restrictions.eq("roleGroupId", id);
			DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserRoleGroup.class);
			criteria.add(a);
			List list = qbcDao.getCriteria(criteria);
			
			if(list.size()==0){
				SimpleExpression a1 = Restrictions.eq("id", Integer.valueOf(id));
				DetachedCriteria criteria1 = DetachedCriteria.forClass(TSystemRolesGroup.class);
				criteria1.add(a1);
				List<TSystemRolesGroup> groupList = qbcDao.getCriteria(criteria1);
				TSystemRolesGroup trg=groupList.get(0);
				
				qbcDao.delete(trg);
			}else{
				request.setAttribute("alert", "当前权限组还有用户存在,请将用户脱离权限组!");
			}
			
			return mapping.findForward("roleGroupList");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
}
